package com.wrupple.muba.catalogs.client.activity.process.task.impl;

import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.activity.process.task.FilterOptionSelectionState;
import com.wrupple.muba.catalogs.client.widgets.FilterOptionSelectionView;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class FilterOptionSelectionStateImpl implements
		FilterOptionSelectionState {

	class FieldCallback extends DataCallback<List<FieldDescriptor>>{

		@Override
		public void execute() {
			lastResult = result.get(0);
			callback.setResultAndFinish(lastResult);
		}
		
	}
	private FieldDescriptor lastResult;
	FilterOptionSelectionView view;
	private StateTransition<FieldDescriptor> callback;
	
	@Inject
	public FilterOptionSelectionStateImpl(FilterOptionSelectionView view) {
		super();
		this.view = view;
	}


	@Override
	public Widget asWidget() {
		return view.asWidget();
	}


	@Override
	public void start(List<FieldDescriptor> options,
			StateTransition<FieldDescriptor> onDone, EventBus bus) {
		FieldCallback viewCallback = new FieldCallback();
		this.callback = onDone;
		view.start(options, viewCallback, bus);
	}

	@Override
	public FieldDescriptor getSelectedFieldDescriptor() {
		return lastResult;
	}

}
